<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools
          :is-root="true"
          :tree-node="company"
          @addDepts="addDepts"
        />
        <!-- 树形结构 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepartments="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDepts
      ref="editDepet"
      :show-dialog.sync="showDialog"
      :tree-node="treeNode"
      @addDepts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDepts from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      // 本来组织结构的数据应该从服务器获取,
      // 但是现在先不管, 写死数据作为测试
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},
      showDialog: false,
      treeNode: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      await getDepartments()
      const res = await getDepartments()
      this.company = {
        name: res.companyName,
        manager: '负责人'
      }
      this.departs = transListToTreeData(res.depts, '')
    },
    addDepts(treeNode) {
      this.showDialog = true
      this.treeNode = treeNode
      // console.log(treeNode)
    },
    editDepts(treeNode) {
      // 父组件调用子组件的方法
      this.$refs.editDepet.getDepartDetail(treeNode.id)
      this.showDialog = true
      this.treeNode = treeNode
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
